package com.ruoyi.quartz.task;

import com.ruoyi.common.constant.AttendanceConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.domain.AttendanceRecordInterface;
import com.ruoyi.domain.TermWorkerQuartz;
import com.ruoyi.quartz.mapper.AttendanceMachineMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * 描述:
 *
 * @author 刘松山
 * @date 2021/11/26 20:15
 */
@Component("attendanceInterfaceTask")
public class AttendanceInterfaceTask {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private AttendanceMachineMapper attendanceMachineMapper;

    private static final Logger LOGGER = LoggerFactory.getLogger(AttendanceInterfaceTask.class);
    public void executeJob(){
        LOGGER.info("考勤机考勤数据同步数据库开始==================");
        List<AttendanceRecordInterface> attendanceRecordList = redisCache.getCacheList("attendanceRecordList");

        List<TermWorkerQuartz> termWorkerList  = redisCache.getCacheList("termWorker");
        List<TermWorkerQuartz> delTermWorkerList  = redisCache.getCacheList("delTermWorker");
        if(null ==termWorkerList || termWorkerList.size() < 1){

            termWorkerList  =attendanceMachineMapper.listTermWorker();
            redisCache.setCacheList("termWorker",termWorkerList);
            redisCache.expire("termWorker", AttendanceConstants.termWorkerExpire);

        }
        if(null ==delTermWorkerList || delTermWorkerList.size() < 1){

            delTermWorkerList  =attendanceMachineMapper.listDelTermWorker();
            redisCache.setCacheList("delTermWorker",delTermWorkerList);
            redisCache.expire("delTermWorker", AttendanceConstants.termWorkerExpire);

        }
        if(attendanceRecordList ==null || attendanceRecordList.size() ==0){
            LOGGER.info("考勤机考勤数据同步数据库结束==================attendanceRecordList:0");
            return ;
        }


       for(AttendanceRecordInterface a:attendanceRecordList){
           int flag =0;
           for(TermWorkerQuartz t:termWorkerList){
             if(t.getIdCard().equals(a.getIdCard())){
                 a.setWorkerName(t.getWorkerName());
                 break;
             }
           }
           if(flag==0){
               a.setWorkerName("未维护");
           }
       }
       //去除重复值 身份证+打卡时间
        List<AttendanceRecordInterface> reultList = attendanceRecordList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
                        () -> new TreeSet<>(Comparator.comparing(o -> o.getIdCard()+"_"+o.getDateTime()))),
                ArrayList::new));
       if(reultList.size() >0){
           attendanceMachineMapper.addAttendanceRecord(reultList);
       }

        redisCache.deleteObject("attendanceRecordList");
        LOGGER.info("考勤机考勤数据同步数据库结束==================");
    }
}
